package com.yisu.expressway.weather.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.yisu.expressway.R;
import com.yisu.expressway.utils.DateUtils;
import com.yisu.expressway.weather.ResUtils;
import com.yisu.expressway.weather.model.DailyWeatherObj;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/10/5 0005.
 */

public class DailyWeatherAdapter extends  RecyclerView.Adapter<DailyWeatherAdapter.WeatherViewHolder>  {

    ArrayList<DailyWeatherObj> mData = new ArrayList<>();
    private Context mContext;

    public DailyWeatherAdapter(Context context){
        this.mContext = context;
    }

    public void setData(ArrayList<DailyWeatherObj> list){
        this.mData = list;
        notifyDataSetChanged();
    }


    @Override
    public WeatherViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View mView = LayoutInflater.from(mContext).inflate(R.layout.daily_weather_item, parent, false);
        return new WeatherViewHolder(mView);
    }

    @Override
    public void onBindViewHolder(WeatherViewHolder holder, int position) {
        if(mData.size() <= position ){
            return;
        }

        final DailyWeatherObj item = mData.get(position);
        switch (position){
            case 0 :
                holder.mTv_date.setText(R.string.today);
                break;
            case 1:
                holder.mTv_date.setText(R.string.tomorrow);
                break;
            case 2:
                holder.mTv_date.setText(DateUtils.getWeek(item.date));
                break;
            default:
                break;
        }

        holder.mTv_weathreName.setText(item.text_day);
        StringBuilder tempRange = new StringBuilder(item.low);
        tempRange.append("/").append(item.high).append("\u2103");
        holder.mTvTempRange.setText(tempRange.toString());
        holder.mIv_icon.setImageResource(ResUtils.getWeatherIcon(item.code_day));

    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    class WeatherViewHolder extends RecyclerView.ViewHolder {

        @Bind(R.id.tv_date) TextView mTv_date;
        @Bind(R.id.iv_weather_status) ImageView mIv_icon;
        @Bind(R.id.tv_weather_name) TextView mTv_weathreName;
        @Bind(R.id.tv_temp_range) TextView mTvTempRange;

        public WeatherViewHolder(View itemView){
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }

}
